﻿

//配置信息设置
var indexItem = 0; //全局变量，当前的题目编号
var yidatishu = 0;
var maxtime = 60 * 60; //一个小时，按秒计算，自己调整!
//var itemCouAll = subject[subject.length - 1].Id; //大题总数
var itemCouAll = 1;

var strst = "", strct = "", strctda = "", strfz = "", strcj = "";
var shiJuanId = 0;
var t; //定时器
window.onload = function () {

    // ----- 填充HTML ------


    //document.getElementById("stzs").innerHTML = subject.length;
    //
    //document.getElementById("dyxm").innerHTML = kaohe11.DuiYuanXingMing;
    //document.getElementById("khmc").innerHTML = kaohe11.KaoHeMingCheng + "模拟测试";
    //document.getElementById("khtk").innerHTML = kaohe11.TiKuMing;
    //document.getElementById("ydts").innerHTML = "0";
    //写入各个大题的div
    var itemChooseHTML =
        `
                    <div>
                        <ul></ul>
                    </div>
                `
    document.getElementById('itemChoose').innerHTML = itemChooseHTML;


    //写入div下的ul li
    //获取每个div下应有多少li
    var orderCou = [];
    for (var i = 0; i < itemCouAll; i++) {
        var m = 0;
        for (var j = 0; j < subject.length; j++) {
            if (subject[j].Id === i + 1) {
                m++;
            }
        }
        orderCou[i] = m;
    }
    for (var j = 0; j < itemCouAll; j++) {
        var optionHTML = '';
        for (var i = 0; i < subject.length; i++) {
            optionHTML += `<li class="noAnswer">${i + 1}</li>`
        }
        document.getElementById("itemChoose").getElementsByTagName("ul")[0].innerHTML = optionHTML;
    }

    //绑定各个li，注意这里的i的作用域，可使用虚拟属性，参考https://www.cnblogs.com/wangyongshf/p/7466799.html
    var liList = document.getElementById('itemChoose').getElementsByTagName('li');
    for (var i = 0; i < liList.length; i++) {
        liList[i].index = i;
        liList[i].onclick = function () {
            indexItem = this.index;
            refresh();
        }
    }

    //设置各个li的class，是否符合回答规范、设置了待查标记
    for (var i = 0; i < liList.length; i++) {

        liList[i].className = YesOrNo(i);

        if (!subject[i].biaoJi) { //设置待查标记
            liList[i].classList.add('markAnswer');
        } else {
            liList[i].classList.remove('markAnswer');
        }

    }

    /** 
    // 开始倒计时
    document.getElementById('suspend').innerHTML = '||';
    t = setInterval("CountDown()", 1000);
    */
    refresh(); //加载题目

}

/** 
// ------ 计时程序 -----
function CountDown() {
    if (maxtime >= 0) {
        //写入HTML
        hours = checkTime(parseInt(maxtime / 60 / 60) % 60);
        minutes = checkTime(parseInt(maxtime / 60) % 60);
        seconds = checkTime(parseInt(maxtime % 60));
        msg = hours + ":" + minutes + ":" + seconds;
        document.getElementById("timer").innerHTML = msg;

        maxtime--;

        if (maxtime === 5 * 60) {
            document.getElementById('information').innerHTML = "还剩5分钟，点击下方按钮继续答题";
            document.getElementById('suspend').click();
        }

    } else {
        clearInterval(t);
        alert("时间到，结束!");
        //开始结算
    }
}

function checkTime(i) { //将0-9的数字前面加上0，例1变为01
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}
*/


//
//题目刷新
//
function refresh() {
    //题目序号与题目类型描述文本
    var itemType = ''; //单选题、多选题
    switch (subject[indexItem].tiXingId) {
        case 1:
            itemType = '填空题';
            break;
        case 2:
            itemType = '单选题';
            break;
        case 4:
            itemType = '判断题';
            break;
        case 3:
            itemType = '多选题';
            break;
    }
    document.getElementById("index").innerHTML = indexItem + 1 + '、【' + itemType + '】&nbsp;&nbsp;[' + subject[indexItem].fenZhi + "分]";



    //题目描述
    var describeText = "";
    describeText += subject[indexItem].tiMu;
    document.getElementById("describe").innerHTML = describeText;

    //答案选项
    var optNum = subject[indexItem].xuanXiang.length;
    var optionText = "";
    var inputType = "";
    var inputName = subject[indexItem].tiXingId;
    switch (subject[indexItem].tiXingId) {
        case 2:
        case 4:
            inputType = "radio";
            break;
        case 3:
            inputType = "checkbox";
            break;
    }

    for (var i = 0; i < optNum; i++) {
        var itemChecked = '';
        if (subject[indexItem].tiXingId === 3) {
            if (subject[indexItem].duiYuanDaAn.charAt(i) !== '_' && subject[indexItem].duiYuanDaAn.charAt(i) !== '') {
                itemChecked = 'checked="checked"';
            }
        } else {
            if (subject[indexItem].duiYuanDaAn === String.fromCharCode(65 + i)) {
                itemChecked = 'checked="checked"';
            }
        }

        optionText +=
            `
                <li>
                    <input type=${inputType} name="opt" id="${i}" value= ${String.fromCharCode(65 + i)} ${itemChecked}/>
                    <i>${String.fromCharCode(65 + i)}</i>
                    <label for="${i}">${subject[indexItem].xuanXiang[i]}</label><br />
                </li>
                `
    }
    document.getElementById("option").innerHTML = optionText;

    //答案解析
    //document.getElementById("knowledge").innerHTML = "【参考答案】：" + subject[indexItem].BiaoZhunDaAn;

    //按钮禁用状态
    if (indexItem === 0) {
        document.getElementById("prevItem").className += "enable";
    } else if (indexItem === subject.length - 1) {
        document.getElementById("nextItem").className += "enable";
    } else {
        document.getElementById("prevItem").className = "";
        document.getElementById("nextItem").className = "";
    }

    //待查状态设置
    if (subject[indexItem].biaoJi == false) { //这里==表示约等于，true也可以视为1
        document.getElementById('markItem').innerHTML = '取消待查';
    } else {
        document.getElementById('markItem').innerHTML = '设置待查';
    }


    //设置左侧答题卡对应题目为当前
    var liList = document.getElementById('itemChoose').getElementsByTagName('li');
    for (var i = 0; i < liList.length; i++) {
        if (i === indexItem) {
            liList[i].classList.add('nowAnswer');
        } else {
            liList[i].classList.remove('nowAnswer');
        }
    }

    BindOpt(); //重新绑定点击选项的事件

}


//上/下一题按钮绑定
document.getElementById("prevItem").onclick = function () {
    if (indexItem) {
        indexItem--;
        refresh();
    }
}

document.getElementById("nextItem").onclick = function () {
    if (indexItem < subject.length - 1) {
        indexItem++;
        refresh();
    }
}


//设置标记按钮绑定
document.getElementById('markItem').onclick = function () {
    if (this.innerHTML === '设置待查') {
        //左侧的class增加markAnswer
        subject[indexItem].biaoJi = 1;
        this.innerHTML = '取消待查';
        document.getElementById('itemChoose').getElementsByTagName('li')[indexItem].classList.add('markAnswer');
    } else { //恢复原始的答题状态
        subject[indexItem].biaoJi = 0;
        this.innerHTML = '设置待查';
        document.getElementById('itemChoose').getElementsByTagName('li')[indexItem].classList.remove('markAnswer');
    }
}

//绑定选项按钮单击事件
function BindOpt() {
    for (var i = 0; i < document.getElementById("option").getElementsByTagName('input').length; i++) {

        document.getElementById("option").getElementsByTagName('input')[i].onchange = function () {
            //将当前答案写到JSon中

            subject[indexItem].duiYuanDaAn = ''; //清除原有答案
            for (var j = 0; j < document.getElementById("option").getElementsByTagName('input').length; j++) {
                if (document.getElementById("option").getElementsByTagName('input')[j].checked) {
                    subject[indexItem].duiYuanDaAn += document.getElementById("option").getElementsByTagName('input')[j].value;
                } else {
                    if (subject[indexItem].tiXingId === '3') {
                        subject[indexItem].duiYuanDaAn += '_';
                    }
                }
            }

            //改变左侧答题卡显示状态
            //
            if (YesOrNo(indexItem) === 'noAnswer') {
                document.getElementById('itemChoose').getElementsByTagName('li')[indexItem].classList.add('noAnswer');
                document.getElementById('itemChoose').getElementsByTagName('li')[indexItem].classList.remove('yesAnswer');
            } else {
                document.getElementById('itemChoose').getElementsByTagName('li')[indexItem].classList.add('yesAnswer');
                document.getElementById('itemChoose').getElementsByTagName('li')[indexItem].classList.remove('noAnswer');
            }
            document.getElementById("ydts").innerHTML = (document.getElementsByClassName("yesAnswer").length - 1);
        }
    }
}

//绑定计时按钮
/**
document.getElementById('suspend').onclick = function () {

    if (this.innerHTML === '||') {
        this.innerHTML = '►';
        clearInterval(t);
    } else {
        this.innerHTML = '||';
        t = setInterval("CountDown()", 1000);
    }
    BlackCover();
}

function BlackCover() {
    var bCover = document.getElementById('blackCover');
    if (bCover.classList.contains('black')) {
        bCover.classList.remove('black');
    } else {
        bCover.classList.add('black');
    }
}

document.getElementById('goOn').onclick = function () {

    document.getElementById('suspend').click();
}
*/

//判断当前题目是否已作答
function YesOrNo(index) {

    if (subject[index].tiXingId === '3') {

        var ans = new Array();
        ans[0] = RepeatNum('_', subject[index].xuanXiang.length);

        for (var i = 0; i < subject[index].xuanXiang.length; i++) {
            ans[i + 1] = RepeatNum('_', i) + String.fromCharCode(65 + i) + RepeatNum('_', subject[index].xuanXiang.length - 1 - i);
        }
        var blockJudge = false; //是否为空
        for (var j = 0; j < ans.length; j++) {
            if (subject[index].duiYuanDaAn === ans[j]) {
                blockJudge = true;
                break;
            }
        }

        if (blockJudge || subject[index].duiYuanDaAn === '') {
            return 'noAnswer';
        } else {
            return 'yesAnswer';
        }

    } else {
        if (subject[index].duiYuanDaAn !== '' && subject[index].duiYuanDaAn !== RepeatNum('_', subject[index].xuanXiang.length)) {
            return 'yesAnswer';
        } else {
            return 'noAnswer';
        }
    }
}


//打印num个str，如times(n,3) nnn
function RepeatNum(str, num) {
    if (num > 1) {
        return str += RepeatNum(str, --num);
    }
    if (num === 1) {
        return str;
    } else {
        return '';
    }
}

document.getElementById('submit').onclick = function () {
    var htmlText = '';
    strst = "";   //清空试题ids
    strct = "";    //清空错题ids
    strctda = "";  //清空错题答案列表
    var account = 0; //总得分
    for (var i = 0; i < subject.length; i++) {
        strst += subject[i].kaoTiId + ",";
        //计算当前题目得分
        var nowAccount = 0;
        if (subject[i].tiXingId === 1) {
            if (subject[i].duiYuanDaAn === '' || subject[i].duiYuanDaAn !== subject[i].biaoZhunDaAn) {
                nowAccount = 0;
                strct += subject[i].kaoTiId + ",";
                strctda += subject[i].duiYuanDaAn.replaceAll("_", "") + ",";
            } else {
                nowAccount = parseInt(subject[i].fenZhi);
            }
        } else if (subject[i].tiXingId === 2) {
            if (subject[i].duiYuanDaAn === '' || subject[i].duiYuanDaAn !== subject[i].biaoZhunDaAn) {
                nowAccount = 0;
                strct += subject[i].kaoTiId + ",";
                strctda += subject[i].duiYuanDaAn.replaceAll("_", "") + ",";
            } else {
                nowAccount = parseInt(subject[i].fenZhi);
                console.log("==" + subject[i].fenZhi);
            }
        } else if (subject[i].tiXingId === 4) {
            if (subject[i].duiYuanDaAn === '' || subject[i].duiYuanDaAn !== subject[i].biaoZhunDaAn) {
                nowAccount = 0;
                strct += subject[i].kaoTiId + ",";
                strctda += subject[i].duiYuanDaAn.replaceAll("_", "") + ",";
            } else {
                nowAccount = parseInt(subject[i].fenZhi);
            }
        } else if (subject[i].tiXingId === 3) {
            if (subject[i].duiYuanDaAn === '' || subject[i].duiYuanDaAn.replaceAll("_", "") !== subject[i].biaoZhunDaAn) {
                nowAccount = 0;
                strct += subject[i].kaoTiId + ",";
                strctda += subject[i].duiYuanDaAn.replaceAll("_", "") + ",";
            } else {
                nowAccount = parseInt(subject[i].fenZhi);
            }
        }
        htmlText += '试题' + i + '&nbsp;&nbsp;答案' + subject[i].biaoZhunDaAn + '&nbsp;&nbsp;回答' + subject[i].duiYuanDaAn.replaceAll('_', '') + ' &nbsp;&nbsp;得分' + nowAccount + '\n';
        account += nowAccount;
    }

    console.log(strst);
    console.log(strct);
    console.log(strctda);
    //获取成绩，并根据成绩计算绩效考核得分
    //strcj = account.toFixed(2);
    strcj = account;
    if (strcj <= 100 && strcj >= 90) {
        strfz = "10";
    }
    if (strcj < 90 && strcj >= 80) {
        strfz = "6";
    }
    if (strcj < 80 && strcj >= 60) {
        strfz = "3";
    }
    if (strcj < 60) {
        strfz = "0";
    }
    console.log("jixiao:" + strcj + "===" + strfz);
    
    if (kaohe11.xingMing == "模拟用户") {
        localStorage.dyxm = kaohe11.xingMing;
        localStorage.khmc = kaohe11.kaoHeMingCheng;
        localStorage.llcj = strcj;
        localStorage.jxdf = strfz;
        localStorage.ctids = strct.substring(0, strct.length - 1);
        localStorage.ctdas = strctda.substring(0, strctda.length - 1);
        window.location.href = "/html/MoNiCuoTi.html"
    } else {
        saveChengJi();
    }

}


function saveChengJi() {
    $.ajax({
        type: "post",
        url: "SaveChengJi",
        //.ashx后端文件（名字不能错）
        //dataType: 'text',
        // 传输回来的数据格式为JSON格式
        data: {
            "kaoheid": kaohe11.id,
            "xingming": kaohe11.xingMing,
            "shiti_ids": strst.substring(0, strst.length - 1),
            "cuoti_ids": strct.substring(0, strct.length - 1),
            "cuoti_das": strctda.substring(0, strctda.length - 1),
            "fenzhi": strfz,
            "chengji": strcj
        },
        // data是传输过去的数据格式（JSON）
        success: function (data) {
            shiJuanId = data;
            console.log("save chengji get data:"+data);
            // 连接成功调用此函数，data为后端传的数据
            if (shiJuanId > 0) {
                console.log("sss" + shiJuanId);
                window.location.href = "/ShiJuan/showCuoTi/" + shiJuanId;
                alert("保存成绩成功！");
            }
        },
        error: function () {
            // 连接失败调用此函数
            alert("保存成绩失败，请截图留存考核成绩！\r\n参考队员：" + kaohe11.xingMing + "\r\n理论成绩：" + strcj +"\r\n绩效得分：" + strfz)
        }
    });
}






